package com.example.dishfo.myapplication.activity.userlogin;

import android.annotation.SuppressLint;
import android.app.ActivityOptions;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.transition.Explode;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dishfo.myapplication.R;
import com.example.dishfo.myapplication.activity.BaseActivity;
import com.example.dishfo.myapplication.activity.content.ContentActivity;
import com.example.dishfo.myapplication.activity.forget.ForgetActivity;
import com.example.dishfo.myapplication.activity.signup.SignUpActivity;
import com.example.dishfo.myapplication.customview.mydialog.WaitDialogFragment;
import com.example.dishfo.myapplication.entity.User;
import com.example.dishfo.myapplication.util.ActivityUtil;

import java.util.Objects;

import javax.annotation.Nonnull;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用于登陆的activity
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.textInputLayout) TextInputLayout userNameInput;
    @BindView(R.id.textInputLayout2) TextInputLayout pwdInput;
    @BindView(R.id.button_login_login) Button button_login;
    @BindView(R.id.textView_login_forget) TextView textViewForget;
    @BindView(R.id.textView_login_signup) TextView textViewSignup;
    @BindString(R.string.login_wait) String loginWait;

    private UserViewModel viewModel;
    private LiveData<User> currentUser;
    private boolean isWait=false;
    private WaitDialogFragment fragment;

    private View.OnClickListener clickListenerLogin = (View v) -> {
        String username=ActivityUtil.getEditText(userNameInput).toString();
        String pwd=ActivityUtil.getEditText(pwdInput).toString();

        userNameInput.setError("");
        userNameInput.setErrorEnabled(false);

        pwdInput.setError("");
        pwdInput.setErrorEnabled(false);

        if(showError(username,pwd)){
            return;
        }
        viewModel.onLogin(username,pwd);
        startWait();
    };

    private View.OnClickListener clickListenerForget=v -> {
        Intent intent=new Intent(MainActivity.this, ForgetActivity.class);
        startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    };

    private View.OnClickListener clickListenerSignUp=v -> {
        Intent intent=new Intent(MainActivity.this, SignUpActivity.class);
        startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityUtil.fullScreen(this);

        ButterKnife.bind(this);
        //生命周期组件初始化
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
        getLifecycle().addObserver(viewModel);
        currentUser=viewModel.getCurrentUser();

        viewModel.getSavUser().observe(this,user -> {
            userNameInput.getEditText().setText(user.getUserName());
            pwdInput.getEditText().setText(user.getPassword());
        });

        currentUser.observe(this,user -> {
            stopWait();
            if((user != null ? user.getId() : null)
                    !=null&&user.getUserName()!=null){
                //登陆成功进行界面跳转
                Intent intent=new Intent(MainActivity.this, ContentActivity.class);
                startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
                Log.d("test","login succeed");
            }else {
                //认为这种情况属于登陆失败
                Toast.makeText(this,"登陆失败",Toast.LENGTH_SHORT).show();
                Log.d("test","login failed");
            }
        });

        fragment=WaitDialogFragment.getInstance(loginWait,dialog -> {
            if(isWait){
                stopWait();
                viewModel.cancelLogin();
            }
        });
        button_login.setOnClickListener(clickListenerLogin);
        textViewForget.setOnClickListener(clickListenerForget);
        textViewSignup.setOnClickListener(clickListenerSignUp);
    }


    private boolean showError(String username,String pwd){

        if(TextUtils.isEmpty(username)){
            userNameInput.setError("用户名为空");
            return true;
        }

        if(TextUtils.isEmpty(pwd)){
            pwdInput.setError("密码为空");
            return true;
        }

        if(username.length()>25){
            userNameInput.setError("用户名过长");
            return true;
        }

        if(pwd.length()<7){
            pwdInput.setError("密码小于7位");
            return true;
        }
        return false;

    }

    private void startWait(){
        isWait=true;
        fragment.show(getSupportFragmentManager(),"waitFialog");
    }

    private void stopWait(){
        isWait=false;
        fragment.dismiss();
    }
}
